package com.tgpms.web.hongbo.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tgpms.common.Result;
import com.tgpms.interfaces.dto.HongBoInterface;
import com.tgpms.web.hongbo.dao.TubeMapper;
import com.tgpms.web.hongbo.entity.Tube;
import com.tgpms.web.hongbo.service.TubeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created By Riven on 2020-9-15
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class TubeServiceImpl implements TubeService {

    @Resource
    private HongBoInterface hongBoInterface;

    @Resource
    private TubeMapper tubeMapper;

    @Override
    public int syncTube() throws UnsupportedEncodingException {
        int returnInt;
        List<Tube> list = new ArrayList<>();
        int ifEmpty = tubeMapper.deleteTube();
        if (ifEmpty != -1) {
            Result tubeResult = hongBoInterface.getTube(-1, -1, null);
            if (tubeResult.getSuccess()) {
                String resData = tubeResult.getData().toString();
                JSONObject resJson = JSONObject.parseObject(resData);
                JSONArray tables = (JSONArray) resJson.get("tables");
                for (Object table : tables) {
                    JSONObject jsonObject = JSONObject.parseObject(table.toString());
                    Tube temp = transform(jsonObject, "D");
                    list.add(temp);
                }
                Result tubeCompleteResult = hongBoInterface.getTubeComplete(-1, -1, null);
                String tubeCompleteData = tubeCompleteResult.getData().toString();
                JSONObject tubeCompleteJson = JSONObject.parseObject(tubeCompleteData);
                JSONArray tubeCompleteTables = (JSONArray) tubeCompleteJson.get("tables");
                for (int i = 0; i < tubeCompleteTables.size(); i++) {
                    JSONObject jsonObject = JSONObject.parseObject(tables.get(i).toString());
                    Tube temp = transform(jsonObject, "C");
                    list.add(temp);
                }
            }
        }
        returnInt = tubeMapper.batchInsert(list);
        return returnInt;
    }

    private Tube transform(JSONObject jsonObject, String type) {
        Tube temp = new Tube();
        temp.setId(jsonObject.get("id").toString());
        temp.setOwner(jsonObject.get("Owner").toString());
        temp.setRoadName(jsonObject.get("Road_Name").toString());
        temp.setJointType(jsonObject.get("Joint_Type").toString());
        temp.setAreaName(jsonObject.get("AreaName").toString());
        temp.setDesigner(jsonObject.get("Designer").toString());
        temp.setStartBsm(jsonObject.get("Start_Bsm").toString());
        temp.setBottomHeight(jsonObject.get("BottomHeight").toString());
        temp.setBuildYear(jsonObject.get("Build_Year").toString());
        temp.setSectionType(jsonObject.get("Section_Type").toString());
        temp.setConstructer(jsonObject.get("Constructer").toString());
        temp.setIsSiphon(jsonObject.get("Is_Siphon").toString());
        temp.setSectionData2(jsonObject.get("Section_Data2").toString());
        temp.setCadMap(jsonObject.get("CADMap").toString());
        temp.setOptionType(jsonObject.get("OptionType").toString());
        temp.setSectionData1(jsonObject.get("Section_Data1").toString());
        temp.setPipeGrade(jsonObject.get("Pipe_Grade").toString());
        temp.setBidCap(jsonObject.get("BidCap").toString());
        temp.setSupervisor(jsonObject.get("Supervisor").toString());
        temp.setPipeStorageTime(jsonObject.get("Pipe_Storage_Time").toString());
        temp.setBury(jsonObject.get("Bury").toString());
        temp.setEndBsm(jsonObject.get("End_Bsm").toString());
        temp.setFillTime(jsonObject.get("Fill_Time").toString());
        temp.setMaterial(jsonObject.get("Material").toString());
        temp.setSupervisor(jsonObject.get("Surveyor").toString());
        temp.setCode(jsonObject.get("Code").toString());
        temp.setPressure(jsonObject.get("Pressure").toString());
        temp.setFillPerson(jsonObject.get("Fill_Person").toString());
        temp.setThickness(jsonObject.get("Thickness").toString());
        temp.setSlopeRate(jsonObject.get("Slope_Rate").toString());
        temp.setBottomHeight2(jsonObject.get("BottomHeight2").toString());
        temp.setLength(jsonObject.get("Length").toString());
        temp.setCadCode(jsonObject.get("CADCode").toString());
        temp.setAreaCode(jsonObject.get("AreaCode").toString());
        temp.setFlowDirection(jsonObject.get("Flow_Direction").toString());
        temp.setWorkArea(jsonObject.get("WorkArea").toString());
        temp.setConnType(jsonObject.get("Conn_Type").toString());
        temp.setLength(jsonObject.get("Length").toString());
        temp.setMemo(jsonObject.get("Memo").toString());
        temp.setType(type);
        temp.setInsertUser("TGPMS");
        temp.setInsertDate(new Date());
        temp.setRowstamp(jsonObject.get("id").toString());
        return temp;
    }

}
